package com.itheima.wechat;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.util.JedisUtils;
import com.itheima.util.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.Map;

/**
 * @author liam
 * @description 验证码控制器
 * @date 2020/3/2-18:41
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
    private JedisUtils jedisUtils;

    @Autowired
    DefaultKaptcha defaultKaptcha;

    @RequestMapping("/code/{deviceId}")
    public void createCode(@PathVariable String deviceId, HttpServletResponse response) throws Exception {
        Assert.notNull(deviceId, "机器码不能为空");
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        //生成文字验证码
        String text = defaultKaptcha.createText();
        //生成图片验证码
        BufferedImage image = defaultKaptcha.createImage(text);
        //生成的验证码写入redis
        jedisUtils.setex(deviceId, 60 * 5, text);
        //获取输出流
        ServletOutputStream out = response.getOutputStream();
        //将图片写回浏览器
        ImageIO.write(image, "JPEG", out);
    }

    @RequestMapping("/send4Order")
    public Result send4Order(String telephone) {
        try {
            //1.获取验证码
            Integer code = ValidateCodeUtils.generateValidateCode(4);
            //2.将验证码短信发送给客户（阿里云服务，收费）
//            SmsUtil.sendSmsCode(telephone,code);
            //3.将验证码存入redis
//            Jedis jedis = jedisPool.getResource();
//            //该方法的第二个参数是这个字段保存在redis数据库中多少秒后将自动删除
//            jedis.setex(telephone + RedisMessageConstant.SENDTYPE_ORDER, 60 * 5 * 10, code);
//            jedis.close();
            jedisUtils.setex(telephone + RedisMessageConstant.SENDTYPE_ORDER, 60 * 5 * 10, code.toString());

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        return Result.success(MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    @RequestMapping("/send4Login")
    public Result send4Login(@RequestBody Map<String, String> map) {
        String telephone = map.get("telephone");
        String deviceId = map.get("deviceId");
        String imgCode = map.get("code");

        String redisImgCode = jedisUtils.get(deviceId);
        if (null == redisImgCode || !redisImgCode.equals(imgCode)) {
            return Result.error("图形验证码输入错误");
        }
        //生成验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        //调用短信接口发送验证
//        SmsUtil.sendSmsCode(telephone,String.valueOf(code));
        System.out.println("code:" + code);

        jedisUtils.setex(telephone + RedisMessageConstant.SENDTYPE_LOGIN , 60 * 60 * 24, String.valueOf(code));

        return Result.success("");
    }
}
